#pragma once

#include <windows.h>
#include <WindowsX.h>
#include <assert.h>
#include "../include/Platform.h"
#include "FontQulity.h"
#include "../util/util.h"
#include "../win32/win32.h"

namespace soy {

void* PointerFromWindow( HWND hWnd );
void SetWindowPointer( HWND hWnd, void* ptr ) ;
void SetWindowID(HWND hWnd, int identifier);



RECT Rect2RECT( Rect& r );
Rect RECT2Rect( RECT r );
bool IsNT();

void LockSection();
void UnlockSection();




template<typename T, int lengthStandard>
class VarBuffer {
	T bufferStandard[lengthStandard];
public:
	T* buffer;
	VarBuffer( size_t length ) : buffer( 0 ) {
		if ( length > lengthStandard ) {
			buffer = new T[length];
		} else {
			buffer = bufferStandard;
		}
	}
	~VarBuffer() {
		if ( buffer != bufferStandard ) {
			delete []buffer;
			buffer = 0;
		}
	}
};

const int stackBufferLength = 10000;
class TextWide : public VarBuffer<wchar_t, stackBufferLength> {
public:
	int tlen;
	TextWide( const char* s, int len, bool unicodeMode, int codePage = 0 ) :
		VarBuffer<wchar_t, stackBufferLength>( len ) {
		if ( unicodeMode ) {
			tlen = UTF16FromUTF8( s, len, buffer, len );
		} else {
			// Support Asian string display in 9x English
			tlen = ::MultiByteToWideChar( codePage, 0, s, len, buffer, len );
		}
	}
};

struct FormatAndMetrics {
	HFONT hfont;

	int extraFontFlag;
	FLOAT yAscent;
	FLOAT yDescent;
	FLOAT yInternalLeading;
	FormatAndMetrics( HFONT hfont_, int extraFontFlag_ ) :
		hfont( hfont_ ),

		extraFontFlag( extraFontFlag_ ), yAscent( 2 ), yDescent( 1 ), yInternalLeading( 0 ) {
	}

	~FormatAndMetrics() {
		if ( hfont ) {
			::DeleteObject( hfont );
		}

		extraFontFlag = 0;
		yAscent = 2;
		yDescent = 1;
		yInternalLeading = 0;
	}
	HFONT HFont();
};


void PlarformInit( HINSTANCE inst );

HINSTANCE GetInstance();



};
